(function ($) {
	$.DHCKeyboard = $.Class.extend({
		init:function(scrollId1,scrollId2,block){
			var self = this;
			self.scrollId1 = scrollId1;
			self.scrollId2 = scrollId2;
			var keyborad = document.body.querySelector('.keyboardPopo');
			if (keyborad) {
				return;
			}
			var keyborad = document.createElement('div');
			keyborad.setAttribute('class','mui-popover keyboardPopo');
			document.body.appendChild(keyborad);
			keyborad.innerHTML = '<div class="rights">All Rights Reserved ©东华医为</div>'+
				'<div class="boardClose nurfont nur-error"></div>'+
				'<div class="btnArea">'+
					'<button class="numberBtn">7</button><button class="numberBtn">8</button><button class="numberBtn">9</button>'+
					'<button class="numberBtn">4</button><button class="numberBtn">5</button><button class="numberBtn">6</button>'+
					'<button class="numberBtn">1</button><button class="numberBtn">2</button><button class="numberBtn">3</button>'+
					'<button class="numberBtn mark">备注</button><button class="numberBtn">0</button><button class="numberBtn">.</button>'+
				'</div>'+
				'<div class="upDownArea">'+
					'<button class="boardBack nurfont pad-backspace"></button>'+
					'<button class="boardUp mui-icon mui-icon-pulldown"></button>'+
					'<button class="boardDown mui-icon mui-icon-pulldown"></button>'+
				'</div>'
				
			var numberBtns = keyborad.querySelectorAll('.numberBtn');
			for (var i = 0; i < numberBtns.length;i++) {
				var numberBtn = numberBtns[i];
				if (numberBtn.innerText == '') {
					continue;
				}
				numberBtn.addEventListener('tap',function(){
					if (this.getAttribute('class').indexOf('mark') != -1) {
						block('mark',this.innerText);
						return
					}
					block('add',this.innerText);
				});
			}
			var del = keyborad.querySelector('.boardBack');
			del.addEventListener('tap',function(){
				block('reduce',this.innerText);
			});
			
			var last = keyborad.querySelector('.boardUp');
			last.addEventListener('tap',function(){
				block('last',this.innerText);
			});
			var next = keyborad.querySelector('.boardDown');
			next.addEventListener('tap',function(){
				block('next',this.innerText);
			});
			var boardClose = keyborad.querySelector('.boardClose');
			boardClose.addEventListener('tap',function(){
				self.hideDHCKeyboard();
				block('close');
			});
		},
		showDHCKeyboard:function(){
			var keyborad = document.body.querySelector('.keyboardPopo');
			keyborad.classList.add('mui-active');
			keyborad.style.display = 'block';
			var scroll1 = document.getElementById(this.scrollId1);
			scroll1.style.paddingBottom = 270*SCALE + 30 +'px'; //多写一个cell 的行高 不抖动
		},
		hideDHCKeyboard:function(){
			var keyborad = document.body.querySelector('.keyboardPopo');
			keyborad.classList.remove('mui-active');
			keyborad.style.display = 'none';
			var scroll1 = document.getElementById(this.scrollId1);
			scroll1.style.paddingBottom = '30px';
		},
		openDHCKeyboardDrag:function(){
			var keyborad = document.body.querySelector('.keyboardPopo');
			var dragDom = keyborad.querySelector('.rights')
			//限制最大宽高，不让滑块出去
			var maxW = screen.width;
			var maxH = screen.height;
			if (maxH > maxW) {
				maxH = screen.width;
				maxW = screen.height;
			}
			if (mui.os.android) {
				maxH -= 385
			}else{
				maxH -= 370;
			}
			maxW -= 280;
			//手指触摸开始，记录div的初始位置
			dragDom.addEventListener('touchstart', function(e) {
				var ev = e || window.event;
			 	var touch = ev.targetTouches[0];
			 	oL = touch.clientX - keyborad.offsetLeft;
			 	oT = touch.clientY - keyborad.offsetTop;
			 	document.addEventListener("touchmove", defaultEvent, false);
			});
			//触摸中的，位置记录
			dragDom.addEventListener('touchmove', function(e) {
			 	var ev = e || window.event;
			 	var touch = ev.targetTouches[0];
			 	var oLeft = touch.clientX - oL;
			 	var oTop = touch.clientY - oT;
			 	if(oLeft < 0) {
				 	oLeft = 0;
			 	}else if (oLeft > maxW) {
			 		oLeft = maxW;
			 	}
			 	if(oTop < 0) {
			 		oTop = 0;
			 	}else if (oTop > maxH) {
			 		oTop = maxH;
			 	}
			 	keyborad.style.left = oLeft + 'px';
			 	keyborad.style.top = oTop + 'px';
			});
			//触摸结束时的处理
			dragDom.addEventListener('touchend', function() {
 				document.removeEventListener("touchmove", defaultEvent);
			});
			//阻止默认事件
			function defaultEvent(e) {
			 	e.preventDefault();
			}
		}
	});
})(mui)